// HotKeyhandlerAppDlg.cpp : implementation file
//

#include "stdafx.h"
#include "HotKeyhandlerApp.h"
#include "HotKeyhandlerAppDlg.h"

#include "HotKeyManager.h"
#include "HotKeyOperations.h"
#include "FileFilterCreator.h"
#include "CAddHotKeyDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

using namespace HotKeyOperation;

// CHotKeyhandlerAppDlg dialog
CHotKeyhandlerAppDlg::CHotKeyhandlerAppDlg(CWnd* pParent /*=NULL*/)
: CDialog(CHotKeyhandlerAppDlg::IDD, pParent)
  {
  m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  }

void CHotKeyhandlerAppDlg::DoDataExchange(CDataExchange* pDX)
  {
  CDialog::DoDataExchange(pDX);
  DDX_Control(pDX, IDC_HK_TABLE, m_hk_table);
  }

BEGIN_MESSAGE_MAP(CHotKeyhandlerAppDlg, CDialog)
  ON_WM_PAINT()
  ON_WM_QUERYDRAGICON()
  ON_BN_CLICKED(IDC_BTN_SAVE, &CHotKeyhandlerAppDlg::OnBnClickedBtnSave)
  ON_BN_CLICKED(IDC_BTN_LOAD, &CHotKeyhandlerAppDlg::OnBnClickedBtnLoad)
  ON_BN_CLICKED(IDC_BTN_ADD, &CHotKeyhandlerAppDlg::OnBnClickedBtnAddHtk)
END_MESSAGE_MAP()


// CHotKeyhandlerAppDlg message handlers

BOOL CHotKeyhandlerAppDlg::OnInitDialog()
  {
  CDialog::OnInitDialog();

  // Set the icon for this dialog.  The framework does this automatically
  //  when the application's main window is not a dialog
  SetIcon(m_hIcon, TRUE);			// Set big icon
  SetIcon(m_hIcon, FALSE);		// Set small icon 

  // TODO: Add extra initialization here

  _InitHKTable();

  return TRUE;  // return TRUE  unless you set the focus to a control
  }

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CHotKeyhandlerAppDlg::OnPaint()
  {
  if (IsIconic())
    {
    CPaintDC dc(this); // device context for painting

    SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

    // Center icon in client rectangle
    int cxIcon = GetSystemMetrics(SM_CXICON);
    int cyIcon = GetSystemMetrics(SM_CYICON);
    CRect rect;
    GetClientRect(&rect);
    int x = (rect.Width() - cxIcon + 1) / 2;
    int y = (rect.Height() - cyIcon + 1) / 2;

    // Draw the icon
    dc.DrawIcon(x, y, m_hIcon);
    }
  else
    {
    CDialog::OnPaint();
    }
  }

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CHotKeyhandlerAppDlg::OnQueryDragIcon()
  {
  return static_cast<HCURSOR>(m_hIcon);
  }

//------------------------------------------------------------------------

BOOL CHotKeyhandlerAppDlg::PreTranslateMessage(MSG *pMsg)
  {
  if (pMsg->message == WM_HOTKEY)
    {
    unsigned int hk_id = (unsigned int)(pMsg->wParam);

    HotKeyManager & hk_manager = HotKeyManager::GetInstance();

    HotKeyManager::HotKeyIterator hk_it = hk_manager.GetHotKey(hk_id);

    if (hk_it != hk_manager.GetEnd())
      (*hk_it)->Handle();
    }
  return CDialog::PreTranslateMessage(pMsg);
  }

//------------------------------------------------------------------------

void CHotKeyhandlerAppDlg::_InitHKTable()
  {
  LVCOLUMN lvColumn;

  lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
  lvColumn.fmt = LVCFMT_CENTER;
  lvColumn.cx = 30;
  lvColumn.pszText = L" #";
  m_hk_table.InsertColumn(0, &lvColumn);

  lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
  lvColumn.fmt = LVCFMT_LEFT;
  lvColumn.cx = 200;
  lvColumn.pszText = L"HotKey name/description";
  m_hk_table.InsertColumn(1, &lvColumn);
  }

//------------------------------------------------------------------------

void CHotKeyhandlerAppDlg::OnBnClickedBtnSave()
  {
  HotKeyManager & hk_manager = HotKeyManager::GetInstance();
  MsgBoxOperation * p_msg_op = new MsgBoxOperation(L"Hello", L"I'm message box.");
  ExecOperation * p_exec_op = new ExecOperation(L"notepad");
  p_exec_op->AddOperation(p_msg_op);
  hk_manager.AddHotKey(VK_TAB, p_exec_op);

  FileFilterCreator file_filter;
  file_filter.AddFilter(L"*.htk", L"HotKey save file");
  file_filter.AddForAllFilter(false);
  file_filter.Create();
  std::wstring filter = file_filter.GetFilterString();

  CFileDialog save_file_dlg(FALSE, L"*.htk", L"HotKeys", 0, filter.c_str());
  if (save_file_dlg.DoModal() == IDOK)
    {
    std::wstring file_name = save_file_dlg.GetPathName();
    hk_manager.Save(file_name);
    }
  }

//------------------------------------------------------------------------

void CHotKeyhandlerAppDlg::OnBnClickedBtnLoad()
  {
  HotKeyManager & hk_manager = HotKeyManager::GetInstance();

  FileFilterCreator file_filter;
  file_filter.AddFilter(L"*.htk", L"HotKey save file");
  file_filter.AddForAllFilter(false);
  file_filter.Create();
  std::wstring filter = file_filter.GetFilterString();

  CFileDialog open_file_dlg(TRUE, L"*.htk", L"HotKeys", 0, filter.c_str());
  if (open_file_dlg.DoModal() == IDOK)
    {
    std::wstring file_name = open_file_dlg.GetPathName();
    hk_manager.Load(file_name);
    }
  }

//------------------------------------------------------------------------

void CHotKeyhandlerAppDlg::OnBnClickedBtnAddHtk()
  {
  CAddHotKeyDlg add_hothey_dlg;
  add_hothey_dlg.ShowWindow(SW_SHOW);
  }

//------------------------------------------------------------------------